package com.xhj.xmly.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.xhj.xmly.R;
import com.xhj.xmly.adapter.DiscoverRecyclerAdapter;
import com.xhj.xmly.adapter.ImageAdapter;
import com.xhj.xmly.adapter.RecommendViewPagerAdapter;
import com.xhj.xmly.bean.Entity;
import com.xhj.xmly.bean.Entity2;
import com.xhj.xmly.bean.FocusImages;
import com.xhj.xmly.bean.RecommendAlbums;
import com.xhj.xmly.bean.SmallIcon;
import com.xhj.xmly.callback.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class Discover_recommend_frag extends Fragment implements Callback<Entity>,View.OnClickListener{

    private DiscoverRecyclerAdapter adapter;
    private List<RecommendAlbums> list;
    private RecyclerView recyclerView;
    private ViewPager recommend_head1;
    private ViewPager recommend_head2;
    private ViewPager recommend_foot;
    private ImageView imageView;
    private List<ImageView>imageList;
    private List<Fragment>fragmentList;
    private RecommendViewPagerAdapter recommendViewPagerAdapter;
    private List<SmallIcon>iconList1;
    private List<SmallIcon>iconList2;
    private ImageAdapter imageAdapter;

    public Discover_recommend_frag() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover_recommend_frag,container,false);
        initView(view);
        initData();
        list = new ArrayList<RecommendAlbums>();
        adapter = new DiscoverRecyclerAdapter(getContext(),list,getFragmentManager());
        recyclerView.setAdapter(adapter);
        return view;
    }

    private void initView(View view) {
        recyclerView = ((RecyclerView) view.findViewById(R.id.discover_recommend_recycler));
        recommend_head1 = ((ViewPager) view.findViewById(R.id.recommend_head1));
        recommend_head2 = ((ViewPager) view.findViewById(R.id.recommend_head2));
        recommend_foot = ((ViewPager) view.findViewById(R.id.recommend_foot));
        recyclerView.setNestedScrollingEnabled(false);

    }

    public void initData() {
        //下载数据
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/mobile/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService service = retrofit.create(HttpService.class);
        service.getText().enqueue(this);
        service.getText2().enqueue(new Callback<Entity2>() {
            @Override
            public void onResponse(Call<Entity2> call, Response<Entity2> response) {
                List<RecommendAlbums>  lists = response.body().getHotRecommends().getList();
                list.addAll(lists);
                adapter.notifyDataSetChanged();


            }

            @Override
            public void onFailure(Call<Entity2> call, Throwable t) {

            }
        });

        //推荐界面的第一个viewpager
        imageList = new ArrayList<>();
        iconList1 = new ArrayList<>();
        iconList2 = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            imageView = new ImageView(getContext());
//            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//            imageView.setImageResource(R.mipmap.ic_launcher);
//            imageList.add(imageView);
//        }
        imageAdapter = new ImageAdapter(imageList);
        recommend_head1.setAdapter(imageAdapter);

        fragmentList = new ArrayList<>();
        recommendViewPagerAdapter = new RecommendViewPagerAdapter(getChildFragmentManager(),fragmentList);
        recommend_head2.setAdapter(recommendViewPagerAdapter);
    }


    @Override
    public void onResponse(Call<Entity> call, Response<Entity> response) {
        Entity body = response.body();
        RecommendAlbums recommendAlbums = body.getEditorRecommendAlbums();
        FocusImages focusImages = body.getFocusImages();
        List<SmallIcon> list = body.getDiscoveryColumns().getList();
        for (int i = 0; i < list.size(); i++) {
            if (i<4){
                iconList1.add(list.get(i));
            }else {
                iconList2.add(list.get(i));
            }
        }
        RecommendViewPagerBaseFragment recommendViewPagerBaseFragment1 = RecommendViewPagerBaseFragment.newInstance(iconList1);
        RecommendViewPagerBaseFragment recommendViewPagerBaseFragment2 = RecommendViewPagerBaseFragment.newInstance(iconList2);
        fragmentList.add(recommendViewPagerBaseFragment1);
        fragmentList.add(recommendViewPagerBaseFragment2);
        for (int i = 0; i < focusImages.getList().size(); i++) {
            imageView = new ImageView(getContext());
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            Picasso.with(getContext()).load(focusImages.getList().get(i).getPic()).into(imageView);
            imageList.add(imageView);
        }
        this.list.add(recommendAlbums);
        adapter.notifyDataSetChanged();
        imageAdapter.notifyDataSetChanged();
        recommendViewPagerAdapter.notifyDataSetChanged();
    }


    @Override
    public void onFailure(Call<Entity> call, Throwable throwable) {
        throwable.printStackTrace();
        Toast.makeText(getContext(), throwable.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {

    }
}
